package club.haomoon.boss.service.impl;

import club.haomoon.boss.service.ISettingSearchService;
import club.haomoon.boss.validate.base.SettingSearchValidate;
import club.haomoon.boss.vo.base.SettingSearchDetailVo;
import club.haomoon.boss.vo.base.SettingSearchObjectVo;
import club.haomoon.core.entity.system.HotSearchData;
import club.haomoon.core.mapper.system.HotSearchDataMapper;
import club.haomoon.core.util.ConfigUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.List;

/**
 * 热门搜索服务实现类
 */
@Service
public class SettingSearchServiceImpl implements ISettingSearchService {

    @Resource
    HotSearchDataMapper hotSearchDataMapper;

    /**
     * 热门搜索详情
     * @return SettingSearchDetailVo
     */
    @Override
    public SettingSearchDetailVo getConfig() {
        Integer status = Integer.parseInt(ConfigUtil.get("hot_search", "status", "0"));
        List<HotSearchData> list = hotSearchDataMapper.selectList(new QueryWrapper<HotSearchData>().orderByDesc("sort"));

        SettingSearchDetailVo vo = new SettingSearchDetailVo();
        vo.setStatus(status);
        vo.setData(list);
        return vo;
    }

    /**
     * 热门搜索新增
     * @param searchValidate 参数
     */
    @Override
    public void setConfig(SettingSearchValidate searchValidate) {
        String isHotSearch = String.valueOf(searchValidate.getStatus());
        ConfigUtil.set("hot_search", "status", isHotSearch);

        hotSearchDataMapper.delete(new QueryWrapper<HotSearchData>().ge("id", 0));
        for (SettingSearchObjectVo vo : searchValidate.getData()) {
            HotSearchData hotSearch = new HotSearchData();
            hotSearch.setName(vo.getName());
            hotSearch.setSort(vo.getSort());
            hotSearchDataMapper.insert(hotSearch);
        }
    }

}
